<template>
  <div class="app-container search-table-box aidex-table">
    <el-card shadow="never" ref="queryRef" style="margin-bottom: 12px;" class="search_card" v-show="showSearch">
      <div class="filter-container">
        <div class="search_box">
          <el-form :model="queryParams" ref="queryForm" label-width="80px" size="mini">
            <el-row :gutter="2">
              <el-col :md="5" >
                <el-form-item prop="searchValues">
                  <el-select
                    v-model="queryParams.searchValues"
                    multiple
                    filterable
                    remote
                    reserve-keyword
                    clearable
                    placeholder="请输入规则名称"
                    :remote-method="remoteSearchValue"
                    @change="handleQuery"
                    :loading="remoteSearchValueLoading"
                    style="width: 415px;margin-left: -78px"
                  >
                    <el-option
                      v-for="item in searchValueOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                      :disabled="item.disabled">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :md="6" align="right">
                <el-form-item>
                  <el-button class="filter-item" type="primary" @click="handleQuery">搜索</el-button>
                  <el-button class="filter-item" style="margin-left: 8px" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-card>
    <el-card shadow="never" >
      <template #header>
        <el-row>
          <el-col :span="8">
            <div class="card-header">
              <el-button disabled type="text">敏感词校验信息</el-button>
            </div>
          </el-col>
          <el-col :span="16">
            <div class="btn_box" align="right" style="float: right;">
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                type="primary"
                icon="el-icon-plus"
                @click="handleAdd"
                size="mini"
                v-hasPermi="['inconfig:wordrule:add']"
              >新增</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-delete"
                type="danger"
                :disabled="multiple"
                @click="handleDelete"
                size="mini"
                v-if="!multiple"
                v-hasPermi="['inconfig:wordrule:remove']"
              >删除</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-download"
                @click="handleExport"
                size="mini"
                v-hasPermi="['inconfig:wordrule:export']"
              >导出</el-button>
              <right-toolbar :showSearch.sync="showSearch" :columns.sync="columns" :columnsKey.sync="columnsKey" @showSearchFun="showSearchFun" @queryTable="getList" :tableKey.sync="tableKey"></right-toolbar>
            </div>
          </el-col>
        </el-row>
      </template>

      <el-table
              border
              stripe
              default-expand-all
              ref="tableRef"
              :key="tableKey"
              :data="wordruleList"
              @selection-change="handleSelectionChange"
              :row-style="rowClass"
              :header-cell-style= "{
                'font-size': '14px !important',
                'font-weight': '500 !important',
                'color': 'rgb(0 0 0)',
                'background-color': 'rgb(205 224 255)',
              }"
              highlight-current-row style="width: 100%;"
              :height="tableHeight"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" align="center" width="55">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="item in columns">
          <el-table-column label="规则名称" align="center" prop="ruleName"  v-if="item.visible && item.label=='敏感词规则'" width="100" show-overflow-tooltip/>
          <el-table-column label="启用状态" align="center" prop="isEnabledName"  v-if="item.visible && item.label=='启用状态'" width="80" show-overflow-tooltip/>
          <el-table-column label="校验内容" align="center" prop="ruleCheckTypeName"  v-if="item.visible && item.label=='校验内容'" width="120" show-overflow-tooltip/>
          <el-table-column label="发票状态" align="center" prop="invoiceStatusName"  v-if="item.visible && item.label=='发票状态'" width="120" show-overflow-tooltip/>
          <el-table-column label="发票金额(含税)限制" align="center" prop="isFeeLimitName"  v-if="item.visible && item.label=='发票金额(含税)限制'" width="160" show-overflow-tooltip/>
          <el-table-column label="发票金额(含税)限制" align="center" prop="isProductLimitName"  v-if="item.visible && item.label=='发票金额(含税)限制'" width="160" show-overflow-tooltip/>
          <el-table-column label="发票类型" align="center" prop="invoiceTypeName"  v-if="item.visible && item.label=='发票类型'" width="100" show-overflow-tooltip/>
        </template>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['inconfig:wordrule:edit']"
            >修改</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button
              type="text"
              style="color: red;"
              @click="handleDelete(scope.row)"
              v-hasPermi="['inconfig:wordrule:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <svg-icon icon-class="search-none" style="font-size: 64px;" />
          <p>暂无数据</p>
        </div>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改敏感词校验对话框 -->
      <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" top="10vh" append-to-body>
        <div style="height: 490px;overflow: auto; padding: 12px 24px;">
          <el-form ref="form" size="mini" :model="form" :rules="rules" label-position="right" label-width="130px">
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="公司:" prop="siteName">
                  <el-input v-model="form.siteName" placeholder="请输入公司" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="规则名称:" prop="ruleName">
                  <el-input v-model="form.ruleName" placeholder="请输入" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="启用状态:" prop="isEnabled">
                  <el-radio-group v-model="form.isEnabled">
                    <el-radio-button
                      v-for="dict in dict.type.bas_common_status"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                    >{{dict.label}}</el-radio-button>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="规则生效范围:" prop="isEnabledName">
                  <el-select
                    v-model="tInRuleSite"
                    multiple
                    filterable
                    clearable
                    placeholder="请选择公司"
                  >
                    <el-option
                      v-for="item in siteList"
                      :key="item.value"
                      :label="item.siteName"
                      :value="item.siteCode">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="校验内容:" prop="ruleCheckType">
                  <el-checkbox v-model="checkAllRuleCheckType" :indeterminate="isRuleCheckTypeIndeterminate" label="全选" size="mini" @change="checkAll($event,'ruleCheckType')" ></el-checkbox>
                  <el-checkbox-group v-model="form.ruleCheckTypes"  @change="handleCheckedChange($event,'checkAllRuleCheckType')">
                    <el-checkbox
                      v-for="dict in dict.type.rule_check_type"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="敏感词范围:" prop="ruleCheckTypeName">
                  <div>
                    <el-button type="info" plain v-for="(w,index) in tInWordList" size="mini" style="margin-right: 2px">
                      {{w.wordName}}
                      <i class="el-icon-close" @click="removeWord(index)"></i>
                    </el-button>
                    <el-input property="请输入" v-if="isinputword" style="width: 100px" v-model="theword" @blur="changeWordInputClose"/>
                    <el-button type="success" icon="el-icon-plus" @click="changeWordInputOpen" v-if="!isinputword">添加敏感词</el-button>
                  </div>
                </el-form-item>
              </el-col>



              <el-col :span="24">
                <el-form-item label="发票类型选择:" prop="invoiceType">
                  <el-checkbox v-model="checkAllInvoiceType" :indeterminate="isInvoiceTypeIndeterminate" label="全选" size="mini" @change="checkAll($event,'invoiceType')"></el-checkbox>
                  <el-checkbox-group v-model="form.invoiceTypes" @change="handleCheckedChange($event,'invoiceType')">
                    <el-checkbox
                      v-for="dict in dict.type.invoice_type"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="发票状态选择:" prop="invoiceStatus">
                  <el-checkbox v-model="checkAllInvoiceStatus" :indeterminate="isInvoiceStatusIndeterminate" label="全选" size="mini" @change="checkAll($event,'invoiceStatus')"></el-checkbox>
                  <el-checkbox-group v-model="form.invoiceStatuses" @change="handleCheckedChange($event,'invoiceStatus')">
                    <el-checkbox
                      v-for="dict in dict.type.invoice_status"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="发票金额(含税)选择:" prop="isFeeLimit">
                  <el-row>
                    <el-col :span="6">
                      <el-radio-group v-model="form.isFeeLimit">
                        <el-radio
                          v-for="dict in dict.type.is_fee_limit"
                          :key="dict.value"
                          :label="dict.value"
                        >{{dict.label}}</el-radio>
                      </el-radio-group>
                    </el-col>
                    <el-col :span="12">
                      <el-input-number v-model="form.feeMin" placeholder="请输入发票起始金额" :disabled="form.isFeeLimit!='1'" style="width: 150px" controls-position="right" :min="0" :precision="8"/>
                      -
                      <el-input-number v-model="form.feeMax" placeholder="请输入发票截止金额" :disabled="form.isFeeLimit!='1'" style="width: 150px" controls-position="right" :min="0" :precision="8"/>
                    </el-col>
                  </el-row>


                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="商品金额(含税)选择:" prop="isProductLimit">
                  <el-row>
                    <el-col :span="6">
                      <el-radio-group v-model="form.isProductLimit">
                        <el-radio
                          v-for="dict in dict.type.is_product_limit"
                          :key="dict.value"
                          :label="dict.value"
                        >{{dict.label}}</el-radio>
                      </el-radio-group>
                    </el-col>
                    <el-col :span="12">
                      <el-input-number v-model="form.priceMin" placeholder="请输入商品金额起始" :disabled="form.isProductLimit!='1'" style="width: 150px" controls-position="right" :min="0" :precision="8"/>
                      -
                      <el-input-number v-model="form.priceMax" placeholder="请输入商品金额截止" :disabled="form.isProductLimit!='1'" style="width: 150px" controls-position="right" :min="0" :precision="8"/>
                    </el-col>
                  </el-row>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary"  size="mini" @click="submitForm">确 定</el-button>
          <el-button  size="mini" @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 查询条件-公司-弹出查询 -->
      <el-dialog v-dialogDrag  :close-on-click-modal="false" title="查询条件-公司查询" :visible.sync="searchsiteCodeDialogOpen" width="800px" append-to-body>
        <div class="dialog_box" style="height:550px;">
            <el-form label-position="left" :model="searchsiteCodeQueryParams" :inline="true"  ref="searchsiteCodeQueryForm" label-width="88px" >
                <el-form-item  prop="searchsiteCodeSearchValue">
                    <el-input
                            v-model="searchsiteCodeQueryParams.searchValue"
                            size="mini"
                            placeholder="请输入编码或名称查询公司"
                            @keyup.enter.native="handleSearchsiteCodeQuery"
                            clearable
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearchsiteCodeQuery">查询</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="searchsiteCodeSelectLoading" :data="searchsiteCodeList"
                      height="400"
                      @row-click="handleSearchsiteCodeRowClick"
                      ref="searchsiteCodeMultipleTable"
                      :row-key="getSearchsiteCodeListRowKey"
                      :row-style="{height:2+'px'}"
                      :header-cell-style= "{
                        'font-size': '14px !important',
                        'font-weight': '500 !important',
                        'background-color':'rgb(241 241 241 / 52%)'
                      }"
                      :cell-style="{padding:'2px 4px'}"
                      border
                      @selection-change="handleSearchsiteCodeSelectionChange">
                <el-table-column type="selection" width="55" align="center"/>
                <!-- 数据 -->
                <el-table-column prop="siteCode" label="组织编码" width="180"></el-table-column>
                <el-table-column prop="siteName" label="组织名称" ></el-table-column>
            <el-table-column prop="propertyTypeName" label="性质" width="150"></el-table-column>
            </el-table>
            <pagination
                    v-show="searchsiteCodeTotal>0"
                    :total="searchsiteCodeTotal"
                    :page.sync="searchsiteCodeQueryParams.pageNum"
                    :limit.sync="searchsiteCodeQueryParams.pageSize"
                    @pagination="getSearchsiteCodeList"
            />
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" type="primary" @click="confirmSearchsiteCode">确 定</el-button>
            <el-button size="mini"   @click="searchsiteCodeCancel">取 消</el-button>
        </div>
    </el-dialog>

      <!-- 编辑-公司-弹出查询 -->
      <el-dialog v-dialogDrag  :close-on-click-modal="false" title="编辑敏感词校验-公司选择" :visible.sync="siteCodeDialogOpen" width="800px" append-to-body>
        <div class="dialog_box" style="height:550px;">
            <el-form label-position="left" :model="siteCodeQueryParams" :inline="true"  ref="siteCodeQueryForm" label-width="88px" >
                <el-form-item  prop="siteCodeSearchValue">
                    <el-input
                            v-model="siteCodeQueryParams.searchValue"
                            size="mini"
                            placeholder="请输入编码或名称查询公司"
                            @keyup.enter.native="handlesiteCodeQuery"
                            clearable
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handlesiteCodeQuery">查询</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="siteCodeSelectLoading" :data="siteCodeList"
                      height="400"
                      @row-click="handlesiteCodeRowClick"
                      ref="siteCodeSingleTable"
                      :row-key="getsiteCodeListRowKey"
                      :row-style="{height:2+'px'}"
                      :header-cell-style= "{
                        'font-size': '14px !important',
                        'font-weight': '500 !important',
                        'background-color':'rgb(241 241 241 / 52%)'
                      }"
                      :cell-style="{padding:'2px 4px'}"
                      border
                      @selection-change="handlesiteCodeSelectionChange">
                <el-table-column label="选择" width="60">
            　　　　<template slot-scope="scope">
            　　　　　　<el-radio v-model="siteCodeDialogRadio" :label="scope.row"><i></i></el-radio>
            　　　　</template>
            　　 </el-table-column>
                <!-- 数据 -->
                <el-table-column prop="companycode" label="公司代码" width="180"></el-table-column>
                <el-table-column prop="companyname" label="公司名称" ></el-table-column>
                <el-table-column prop="remark" label="备注" width="120"></el-table-column>
            </el-table>
            <pagination
                    v-show="siteCodeTotal>0"
                    :total="siteCodeTotal"
                    :page.sync="siteCodeQueryParams.pageNum"
                    :limit.sync="siteCodeQueryParams.pageSize"
                    @pagination="getsiteCodeList"
            />
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" type="primary" @click="confirmsiteCode">确 定</el-button>
            <el-button size="mini" @click="siteCodeCancel">取 消</el-button>
        </div>
    </el-dialog>

    </el-card>
  </div>
</template>

<script>
import { listWordrule, getWordrule, delWordrule, addWordrule, updateWordrule } from "@/api/inconfig/wordrule";
import { listCompany as listcompany, getCompany as getcompany } from "@/api/core/company";
/* 城市列表 **/
import { provinceList, cityList, districtList } from "@/api/system/city";
import { listSite } from "@/api/system/site";

export default {
  name: "Wordrule",
  dicts: ['is_fee_limit','is_product_limit', 'bas_common_status', 'rule_check_type', 'invoice_status', 'invoice_type'],
  data() {
    return {
      advanced: false,
      tableHeight: this.getInitTableHeight(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTInWord: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 敏感词校验表格数据
      wordruleList: [],
      // 敏感词表格数据
      tInWordList: [],
      // 公司范围
      tInRuleSite: [],
      // 公司列表
      siteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{

        },
        searchValues: [],
        ruleId: null,
        custId: null,
        siteCode: null,
        siteName: null,
        ruleName: null,
        isEnabled: null,
        isEnabledName: null,
        ruleCheckType: null,
        ruleCheckTypeName: null,
        invoiceStatus: null,
        invoiceStatusName: null,
        isFeeLimit: "1",
        isFeeLimitName: null,
        feeMin: null,
        feeMax: null,
        isProductLimit: "1",
        isProductLimitName: null,
        priceMin: null,
        priceMax: null,
        invoiceType: null,
        invoiceTypeName: null,
        checkRuleType: null,
        checkRuleTypeName: null,
      },
      // 远程查询条件加载
      remoteSearchValueLoading: false,
      // 关键字查询条件
      searchValueOptions:[

      ],
      isinputword: false,
      theword: '',
      // 表单参数
      form: {
          invoiceTypes : [],
          ruleCheckTypes : [],
          invoiceStatuses : [],
          isEnabled: "1",
          isFeeLimit: "1",
          isProductLimit: "1",
      },
      // 表单校验
      rules: {
      },
      checkAllRuleCheckType: false,
      isRuleCheckTypeIndeterminate: false,
      checkAllInvoiceType: false,
      isInvoiceTypeIndeterminate: false,
      checkAllInvoiceStatus: false,
      isInvoiceStatusIndeterminate: false,
      tableKey: 9,
      //
      columnsKey: 'inwordrule',
      // 自定义列
      columns: [
          {
              key: 1,
              label: `规则名称`,
              visible: true
          },
          {
              key: 2,
              label: `启用状态`,
              visible: true
          },
          {
              key: 3,
              label: `校验内容`,
              visible: true
          },
          {
              key: 4,
              label: `发票状态`,
              visible: true
          },
          {
              key: 5,
              label: `发票金额(含税)限制`,
              visible: true
          },
          {
              key: 6,
              label: `发票金额(含税)限制`,
              visible: true
          },
          {
              key: 7,
              label: `发票类型`,
              visible: true
          }
      ],
      /* ======================================省市区======================================  */
      // 省
      provinceId: null,
      provinceList:[],
      // 市
      cityId: null,
      cityList:[],
      // 区
      districtId: null,
      districtList:[],

      /* ======================================查询条件-公司代码选择======================================  */
      searchsiteCodes:[],
      searchsiteCodenames:[],
      searchsiteCodeTotal: 0,
      searchsiteCodeList: [],
      searchsiteCodeDialogOpen: false,
      searchsiteCodeSelectLoading: false,
      searchsiteCodeQueryParams: {
          searchValue: null,
          pageNum: 1,
          pageSize: 20,
      },
      /* ======================================编辑-公司代码选择======================================  */
      siteCodename: null,
      siteCodeTotal: 0,
      siteCodeList: [],
      siteCodeDialogRadio: null,
      siteCodeDialogOpen: false,
      siteCodeSelectLoading: false,
      siteCodeQueryParams: {
          searchValue: null,
          pageNum: 1,
          pageSize: 20,
      },
    };
  },
  created() {
    this.getList();
    this.getSiteList();
  },
  methods: {
    /** 行样式 */
    rowClass({ row, rowIndex }) {
      if (this.ids.includes(row.id)) {
          return { background: "#d9edf7", height:'20px' };
      } else {
          return { height:'20px'};
      }
    },
   /** 隐藏搜索按钮操作 */
   showSearchFun(isShowSearch){
      this.showSearch = isShowSearch
      let oldHeight = this.$refs.queryRef.$el.offsetHeight
      if(!isShowSearch){
          //当前是显示状态
          oldHeight = oldHeight + 12
      }else{
          oldHeight = oldHeight - 12
      }
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 展开按钮操作 */
    toggleAdvanced() {
      const oldHeight = this.$refs.queryRef.$el.offsetHeight
      this.advanced = !this.advanced
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 查询敏感词校验列表 */
    getList() {
      this.loading = true;
      listWordrule(this.queryParams).then(response => {
        this.wordruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ruleId: null,
        custId: null,
        siteCode: null,
        siteName: null,
        ruleName: null,
        isEnabled: "1",
        isEnabledName: null,
        ruleCheckType: null,
        ruleCheckTypeName: null,
        invoiceStatus: null,
        invoiceStatusName: null,
        isFeeLimit: "1",
        isFeeLimitName: null,
        feeMin: null,
        feeMax: null,
        isProductLimit: "1",
        isProductLimitName: null,
        priceMin: null,
        priceMax: null,
        invoiceType: null,
        invoiceTypeName: null,
        checkRuleType: null,
        checkRuleTypeName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        invoiceTypes : [],
        ruleCheckTypes : [],
        invoiceStatuses : [],
      };
      this.tInWordList = [];
      this.resetForm("form");
    },

      /** 查询公司列表 */
      getSiteList() {
          this.loading = true;
          let siteQueryParams = {
              pageNum: 1,
              pageSize: 99999,
          }
          listSite(this.siteQueryParams).then(response => {
              this.siteList = response.rows;
          });
      },

      /* ※※ 查询 省市区 相关方法 start ※※ */
      // 获取省
      getProvinceList(){
          provinceList().then(response => {
              this.provinceList = response.data;
          });
      },
      // 获取市
      getCityList(){
          console.log(this.provinceId);
          cityList( {"parentId" : this.provinceId } ).then(response => {
              this.cityList = response.data;
          });
      },
      // 获取区县
      getDistrictList(){
          districtList( {"parentId" : this.cityId } ).then(response => {
              this.districtList = response.data;
          });
      },
      // 选中省份
      handleChangeProvince(val){
          // 给表单省字段赋值
          this.form.provinceid = val;
          let province = {};
          province = this.provinceList.find((item)=>{
              return item.id === val;
          });
          this.form.province = province.name;
          // 城市数据清空、区县数据清空;
          this.cityList = [];
          this.districtList = [];
          // 已选市、区数据清空
          this.cityId = null;
          this.districtId = null;
          // 表单城市、区县数据清空
          this.form.cityid = null;
          this.form.city = null;
          this.form.areaid = null;
          this.form.area = null;
          // 获取城市数据
          this.getCityList();
      },
      // 清空省份
      handleClearProvince(){
          // 城市数据清空、区县数据清空;
          this.cityList = [];
          this.districtList = [];
          // 已选市、区数据清空
          this.cityId = null;
          this.districtId = null;
          // 表单城市、区县数据清空
          this.form.cityid = null;
          this.form.city = null;
          this.form.areaid = null;
          this.form.area = null;
      },
      // 选中城市
      handleChangeCity(val){
          // 给表单市字段赋值
          this.form.cityid = val;
          let city = {};
          city = this.cityList.find((item)=>{
              return item.id === val;
          });
          this.form.city = city.name;
          // 区县数据清空
          this.districtList = [];
          // 已选区县清空
          this.districtId = null;
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
          // 获取城市数据
          this.getDistrictList();
      },
      // 清空省份
      handleClearCity(){
          // 区县数据清空
          this.districtList = [];
          // 已选区县清空
          this.districtId = null;
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
      },
      // 选中区县
      handleChangeDistrict(val){
          // 给表单省字段赋值
          this.form.areaid = val;
          let district = {};
          district = this.districtList.find((item)=>{
              return item.id === val;
          });
          this.form.area = district.name;
      },
      // 清空省份
      handleClearDistrict(){
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
      },

      /* ※※ 查询 省市区 相关方法 end ※※ */




    /* ※※ 查询 公司代码 相关方法 start ※※ */
    /** ① 弹出查询条件-公司代码 */
    openSearchsiteCodeDialogOpen(){
        this.searchsiteCodeDialogOpen = true;
        // 默认不查询
    },
    /** ② 单击行 **/
    handleSearchsiteCodeRowClick(row, column, event){
        this.$refs.searchsiteCodeSingleTable.toggleRowSelection(row);
    },
    /** ③ 行KEY **/
    getSearchsiteCodeListRowKey(row) {
        return row.companycode
    },
    /** ④ 选中公司代码 **/
    handleSearchsiteCodeSelectionChange(selection) {
        this.searchsiteCodes = selection.map(item => item.siteCode);
        this.searchsiteCodenames = selection.map(item => item.siteName);
    },
    /** ⑤ 点击查询按钮 **/
    handleSearchsiteCodeQuery(){
        this.searchsiteCodeQueryParams.pageNum = 1;
        this.getSearchsiteCodeList();
    },
    /** ⑥ 查询 **/
    getSearchsiteCodeList(){
        this.searchsiteCodeSelectLoading = true;
        listcompany(this.searchsiteCodeQueryParams).then(response => {
            this.searchsiteCodeList = response.rows;
            this.searchsiteCodeTotal = response.total;
            this.searchsiteCodeSelectLoading = false;
        });
    },
    /** ⑦ 点击确认查询 **/
    confirmSearchsiteCode() {
        if(this.searchsiteCodes.length > 0){
            this.queryParams.params.siteCodes = this.searchsiteCodes.join(',');
            this.queryParams.params.companynames = this.searchsiteCodenames.join(',');
        }
        // 关闭查询弹出框
        this.searchsiteCodeCancel();
    },
    /** ⑧ 点击关闭查询弹出框 **/
    searchsiteCodeCancel() {
        this.searchsiteCodeDialogOpen = false;
    },
    /* ※※ 查询 公司代码 相关方法 end ※※ */


    /* ※※ 编辑 公司代码 相关方法 start ※※ */
    /** ① 弹出编辑内容-公司代码 */
    opensiteCodeDialogOpen(){
        this.siteCodeDialogOpen = true;
        this.getsiteCodeList();
    },
    /** ② 单击行 **/
    handlesiteCodeRowClick(row, column, event){
        this.$refs.siteCodeSingleTable.toggleRowSelection(row);
    },
    /** ③ 行KEY **/
    getsiteCodeListRowKey(row) {
        return row.companycode
    },
    /** ④ 选中公司代码 **/
    handlesiteCodeSelectionChange(selection) {
        if (selection[0]) {
            this.siteCodeDialogRadio = selection[0]
            this.form.companycode = selection[0].companycode;
            this.form.companyname = selection[0].companyname;
        }
    },
    /** ⑤ 点击查询按钮 **/
    handlesiteCodeQuery(){
        this.siteCodeQueryParams.pageNum = 1;
        this.getsiteCodeList();
    },
    /** ⑥ 查询 **/
    getsiteCodeList(){
        this.siteCodeSelectLoading = true;
        listcompany(this.siteCodeQueryParams).then(response => {
            this.siteCodeList = response.rows;
            this.siteCodeTotal = response.total;
            this.siteCodeSelectLoading = false;
        });
    },
    /** ⑦ 点击确认查询 **/
    confirmsiteCode() {
        this.siteCodeCancel();
    },
    /** ⑧ 点击关闭查询弹出框 **/
    siteCodeCancel() {
        this.siteCodeDialogOpen = false;
    },
    /* ※※ 编辑 公司代码 相关方法 end ※※ */


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加敏感词预警规则配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWordrule(id).then(response => {
        this.form = response.data;
        this.form.ruleCheckType = this.form.ruleCheckType.split(",");
        this.form.invoiceType = this.form.invoiceType.split(",");
        this.tInWordList = response.data.tInWordList;
        this.open = true;
        this.title = "修改敏感词校验";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.ruleCheckType = this.form.ruleCheckType.join(",");
          this.form.invoiceType = this.form.invoiceType.join(",");
          this.form.tInWordList = this.tInWordList;
          this.form.tInRuleSite = this.tInRuleSite;
          if (this.form.id != null) {
            updateWordrule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWordrule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除敏感词校验编号为"' + ids + '"的数据项？').then(function() {
        return delWordrule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 敏感词序号 */
    rowTInWordIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 敏感词添加按钮操作 */
    handleAddTInWord() {
      let obj = {};
      obj.entryId = "";
      obj.custId = "";
      obj.siteCode = "";
      obj.siteName = "";
      obj.wordName = "";
      this.tInWordList.push(obj);
    },
    /** 敏感词删除按钮操作 */
    handleDeleteTInWord() {
      if (this.checkedTInWord.length == 0) {
        this.$modal.msgError("请先选择要删除的敏感词数据");
      } else {
        const tInWordList = this.tInWordList;
        const checkedTInWord = this.checkedTInWord;
        this.tInWordList = tInWordList.filter(function(item) {
          return checkedTInWord.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTInWordSelectionChange(selection) {
      this.checkedTInWord = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inconfig/wordrule/export', {
        ...this.queryParams
      }, `wordrule_${new Date().getTime()}.xlsx`)
    },
    changeWordInputOpen(){
        this.isinputword = true;
    },
    changeWordInputClose(){
        if(this.theword){
            // 添加一个敏感词模块
            let wordObj = {
                id: null,
                entryId: null,
                ruleId: null,
                custId: null,
                siteCode: null,
                siteName: null,
                wordName: this.theword
            }
            this.tInWordList.push(wordObj);
            // 当前输入框值改为空
            this.theword = '';
        }

        this.isinputword = false;

    },
    removeWord(index){
        this.tInWordList.splice(index, 1);
    },

    checkAll(val, column){
          if(val){
              switch (column) {
                  case 'invoiceType':
                      this.dict.type.invoice_type.map((dict, index) => {
                          this.form.invoiceTypes.push(dict.value)
                      });
                      break;
                  case 'ruleCheckType':
                      this.dict.type.rule_check_type.map((dict, index) => {
                          this.form.ruleCheckTypes.push(dict.value)
                      });
                      break;
                  case 'invoiceStatus':
                      this.dict.type.invoice_status.map((dict, index) => {
                          this.form.invoiceStatuses.push(dict.value)
                      });
                      break;
              }

          } else {
              switch (column) {
                  case 'invoiceType':
                      this.form.invoiceTypes = []
                      break;
                  case 'ruleCheckType':
                      this.form.ruleCheckTypes = []
                      break;
                  case 'invoiceStatus':
                      this.form.invoiceStatuses = []
                      break;
              }
          }
          switch (column) {
              case 'invoiceType':
                  this.isInvoiceTypeIndeterminate = false;
                  break;
              case 'ruleCheckType':
                  this.isRuleCheckTypeIndeterminate = false;
                  break;
              case 'invoiceStatus':
                  this.isInvoiceStatusIndeterminate = false;
                  break;
          }
      },
      handleCheckedChange(value, column) {
          let checkedCount = value.length;
          switch (column) {
              case 'invoiceType':
                  this.checkAllInvoiceType = checkedCount === this.dict.type.invoice_type.length;
                  this.isInvoiceTypeIndeterminate = checkedCount > 0 && checkedCount < this.dict.type.invoice_type.length;
                  break;
              case 'ruleCheckType':
                  this.checkAllRuleCheckType = checkedCount === this.dict.type.rule_check_type.length;
                  this.isRuleCheckTypeIndeterminate = checkedCount > 0 && checkedCount < this.dict.type.rule_check_type.length;
                  break;
              case 'invoiceStatus':
                  this.checkAllInvoiceStatus = checkedCount === this.dict.type.invoice_status.length;
                  this.isInvoiceStatusIndeterminate = checkedCount > 0 && checkedCount < this.dict.type.invoice_status.length;
                  break;
          }
      },


      /** 动态加载关键字查询条件 */
      remoteSearchValue(query) {
          this.remoteSearchValueLoading = true;
          if (query !== '') {
              this.searchValueOptions = [
                  {value: '规则名称:' + query ,label: '规则名称:' + query },
              ];
              this.searchValueOptions.map(item => {
                  this.queryParams.searchValues.map((item2, index) => {
                      if(item.value.startsWith('规则名称') && item2.startsWith('规则名称')){
                          item.disabled = true;
                      }
                  })
              });
          } else {
              this.searchValueOptions = [];
          }
          this.remoteSearchValueLoading = false;
      },
  }
};
</script>
